<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="productForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="productForm.title" />
      </el-form-item>
      <el-form-item label="目录" prop="catalogue_identity">
        <el-select v-model="productForm.catalogue_identity" placeholder="请选择">
          <el-option
            v-for="item in catalogeOptions"
            :key="item.ID"
            :label="item.title"
            :value="item.ID"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="封面图片">
        <Upload v-model="productForm.avatar" />
      </el-form-item>
      <el-form-item label="产品画册">
        <Uploads v-model="productForm.avatars" @childFn="parentFn" />
      </el-form-item>
      <el-form-item label="摘要">
        <el-input v-model="productForm.description" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入产品介绍" />
      </el-form-item>

      <el-form-item prop="介绍" style="margin-bottom: 30px;">
        <ckeditor value="Hello, World!" @ready="onEditorReady" />
      </el-form-item>
      <el-form-item label="市场价" prop="original_price">
        <el-input v-model="productForm.original_price" />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="productForm.price" />
      </el-form-item>
      <el-form-item label="SEO标题" prop="seotitle">
        <el-input v-model="productForm.seotitle" />
      </el-form-item>
      <el-form-item label="SEO关键字">
        <el-input v-model="productForm.seokeyword" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="seo关键字，不超过80个字符" />
      </el-form-item>
      <el-form-item label="seo描述">
        <el-input v-model="productForm.seodescription" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="seo描述，不超过80个字符" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="productForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="productForm.status.value === 5" type="danger" @click="handleDelete(temp)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveProduct, removeProductByID, getProduct } from '@/api/supply/product'
import { getCataloguesAvabile } from '@/api/supply/catalogue'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import CKEditor from 'ckeditor4-vue'
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import Uploads from '@/components/Upload/MultipleImage' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload, Uploads, ckeditor: CKEditor.component },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      catalogeOptions: [],
      productForm: {
        title: '',
        description: '',
        avatars: [],
        content: '',
        price: '',
        original_price: '',
        catalogue_identity: 0,
        remark: '',
        seotitle: '',
        seokeyword: '',
        seodescription: '',
        status: {
          label: '',
          value: 1
        }
      },
      rules: {

      }
    }
  },
  created() {
    this.getCatalogueList()
    let productID = this.$route.query && this.$route.query.productID
    if (productID !== undefined) {
      productID = parseInt(productID)
      if (!isNaN(productID)) {
        this.listLoading = true
        const formData = {
          'productID': productID
        }
        getProduct(formData).then(response => {
          if (response.status === 200) {
            this.productForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(productID)
  },
  methods: {
    onEditorReady(CKEDITOR) {
      CKEDITOR.on('paste', function(e) {
        console.log(e.editor)
        console.log('hello ckeditor')
      })
    },
    parentFn(payload) {
      this.productForm.avatars.push({ path: payload })
      console.log(this.productForm)
    },
    getCatalogueList() {
      getCataloguesAvabile().then(response => {
        if (response.status === 200) {
          this.catalogeOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'product' })
    },
    draftData() {
      this.listLoading = true
      const _productForm = this.productForm
      _productForm.status = 1
      if (_productForm.ID > 0) {
        _productForm.productID = _productForm.ID
      }
      saveProduct(_productForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'product' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    createData() {
      this.listLoading = true
      const _productForm = this.productForm
      _productForm.status = 6
      if (_productForm.ID > 0) {
        _productForm.productID = _productForm.ID
      }
      saveProduct(_productForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'product' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.listLoading = true

      removeProductByID(this.listQuery).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'product' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
